# 对requests方法封装
import configparser
import logging

import jsonpath
import requests
import yaml


class Base:
    def __init__(self):
        pass

    # get
    def do_get(self, url, params=None, **kwargs):
        """
        封装get请求_kw
        :param url:
        :param params:
        :param kwargs:
        :return:
        """
        r = requests.get(url=url, params=params, **kwargs)
        code = r.status_code
        try:
            body = r.json()
        except:
            body = r.text
        res = dict()
        res['code'] = code
        res['body'] = body
        return res

    # post
    def do_post(self, url, jsondata=None, **kwargs):
        """
        封装post请求_kw
        :param url:
        :param jsondata:    没有单独准备data的值，而是准备的json参数的值
        :param kwargs:
        :return:
        """
        r = requests.post(url=url, json=jsondata, **kwargs)
        code = r.status_code
        try:
            body = r.json()
        except:
            body = r.text
        res = dict()
        res['code'] = code
        res['body'] = body
        return res

    # 自定义请求
    def do_request(self, method='get', url=None, jsondata=None, token=None):
        """
        请求方法_kw
        :param url:
        :param method:
        :param jsondata:
        :param cookies:
        :return: 接口返回值，dict格式
        """
        if method == 'get':
            r = requests.get(url, json=jsondata, cookies=token)
            code = r.status_code
        elif method == 'post':
            r = requests.post(url, json=jsondata, cookies=token)
            code = r.status_code
        elif method == 'delete':
            r = requests.delete(url)
            code = r.status_code
        else:
            print("未知的请求方式！")
        try:
            body = r.json()
        except Exception as _:
            body = r.text
        res = dict()
        res['code'] = code
        res['body'] = body
        return res
